/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.swing;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.undo.*;

/**
 * A JEditorPane with added support for Undo / Redo actions.
 *
 * @author Francisco Morero Peyrona
 */

public class JxEditorPane extends JEditorPane
{
   protected UndoManager undo = new UndoManager();

   //-------------------------------------------------------------------------//

   public JxEditorPane()
   {
      getDocument().addUndoableEditListener( new UndoListener() );
   }

   public void setDocument( Document doc )
   {
      super.setDocument( doc );

      doc.addUndoableEditListener( new UndoListener() );
   }

   public void undo()
   {
      if( this.undo.canUndo() )
         this.undo.undo();
   }

   public void redo()
   {
      if( this.undo.canRedo() )
         this.undo.redo();
   }

   public boolean canUndo()
   {
      return this.undo.canUndo();
   }

   public boolean canRedo()
   {
      return this.undo.canRedo();
   }
   
   public void clearUndoManager()
   {
      this.undo.discardAllEdits();
   }   

   //-------------------------------------------------------------------------//
   // INNER CLASS: UndoableEditListener
   //-------------------------------------------------------------------------//
   protected class UndoListener implements UndoableEditListener
   {
      public void undoableEditHappened( UndoableEditEvent evt )
      { 
         JxEditorPane.this.undo.addEdit( evt.getEdit() );
      }
   }
}
